package cn.com.gridinfo.sooc.core.my.setting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.login.activity.forgetpwd.BackPwdMiBaoActivity;
import cn.com.gridinfo.sooc.core.login.activity.forgetpwd.BackPwdTelActivity;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 已经设置密保问题，选择验证方式
 * Created by 刘晓晨 on 2016/1/14.
 */
public class YZModeActivity extends BaseSoocActivity {
    @InjectView(R.id.miBao)//密保的图片
            ImageView miBao;
    @InjectView(R.id.goToMiBao)// 密保背景圆环
            ImageView goToMiBao;
    @InjectView(R.id.cellPhone)// 手机验证的图片
            ImageView cellPhone;
    @InjectView(R.id.goToCellphone)// 手机验证的圆环
            ImageView goToCellphone;
    @InjectView(R.id.click_yz)//密保验证的背景填充 ，用来点击显示动画
            ImageView clickYz;
    @InjectView(R.id.click_yzPhone)//手机号验证的背景填充，用来点击显示动画
            ImageView clickYzPhone;
    @InjectView(R.id.check_mibao)//用来监听点击密保
    RelativeLayout checkMibao;
    @InjectView(R.id.check_phone)// 用来监听点击手机
    RelativeLayout checkPhone;
    @InjectView(R.id.tv_yzsj)//验证手机，未绑定手机时显示“验证手机（未绑定）”
    TextView tvYzsj;

    private Handler mHandler = new Handler();
    private String mibao="1";//标志是否设置了密保问题 0:没有设置，1设置了
    private String mobilePhone="1";//标志是否绑定了手机  0：没有绑定 1绑定了
    private String ycPhoneNumber;//隐藏后的手机号码
    @InjectView(R.id.layout)
    LinearLayout layout;//整个布局
    @InjectView(R.id.toolbar)
    Toolbar toolbar;//toolBar

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_setting_yzmode);
        ButterKnife.inject(this);
        Log.d("用户绑定的手机号" + getIntent().getStringExtra("BoundedPhoneNumber"));
        //设置背景色
        layout.setBackgroundResource(R.mipmap.my_yz_mibao_back);
        toolbar.setBackgroundColor(getResources().getColor(R.color.transparent));

//        if (Arad.preferences.getString("miBaoOnBound").equals("miBaoSure")) {
//            //判断是否被验证过，验证通过了直接跳转到修改界面，不显示圆环
//            goToMiBao.setVisibility(View.GONE);
//        }
//        if (Arad.preferences.getString("phoneOnBound").equals("phoneSure")) {
//            //判断是否被验证过，验证通过了直接跳转到修改界面，不显示圆环
//            goToCellphone.setVisibility(View.GONE);
//        }
        if (StringUtils.isNull(getIntent().getStringExtra("BoundedPhoneNumber"))) {//没有绑定手机号
            mobilePhone="0";//获取标志
            cellPhone.setAlpha(76);
            goToCellphone.setBackgroundResource(R.drawable.back_circle_30);
            tvYzsj.setText("验证手机 ( 未绑定 )");
            checkPhone.setEnabled(false);
        }else {
            tvYzsj.setText("验证手机");
            String beforePhone = getIntent().getStringExtra("BoundedPhoneNumber").substring(0, 3);//拿到电话号码前3位
            String afterPhone = getIntent().getStringExtra("BoundedPhoneNumber").substring(7);//拿到电话号码的后4位
            ycPhoneNumber=beforePhone + "****" + afterPhone;
        }
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "修改密保问题";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //ToolBar的back键
    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }

    //点击验证原密保问题
    @OnClick(R.id.check_mibao)
    void goToMiBao() {
        if(mibao.equals("0")){
            MessageUtils.showImageShortToast(this,"用户没有设置密保问题");

        }else {
            Animation scale = AnimationUtils.loadAnimation(YZModeActivity.this, R.anim.expand);
            clickYz.startAnimation(scale);//设置动画效果
            scale.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //动画结束后跳转
                            goToActivity();
                        }
                    }, 0);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
        }
    }

    //跳转到验证密保界面
    private void goToActivity() {
        Intent intent;
//        if (Arad.preferences.getString("miBaoOnBound").equals("miBaoSure")) {
//            intent = new Intent(this, MiBaoActivity.class);
//            finish();
//        }else {
            intent = new Intent(this, BackPwdMiBaoActivity.class);

//        }
        //拿到节点 传递出去
        intent.putExtra("xtdz", Arad.preferences.getString(LocalMark.SERVER_URL.getName()));
        //拿到uid  传递出去
        intent.putExtra("uid", Arad.preferences.getString(LocalMark.USER_UID.getName()));
        intent.putExtra("way", "000");//用来标志我是从设置点击进来的
        startActivityForResult(intent, 123);
        AnimUtil.intentSlidIn(this);
    }

    // 点击验证手机号
    @OnClick(R.id.check_phone)
    void GoToCellphone() {
        if(mobilePhone.equals("0")){
            MessageUtils.showImageShortToast(this,"用户没有绑定手机号");

        }else {
            Animation scale = AnimationUtils.loadAnimation(YZModeActivity.this, R.anim.expand);
            clickYzPhone.startAnimation(scale);//设置动画效果
            scale.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //动画执行完后再跳转
                            goToActivity2();
                        }
                    }, 0);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
        }
    }

    //跳转到验证手机界面
    private void goToActivity2() {
        Intent intent;
//        if (Arad.preferences.getString("phoneOnBound").equals("phoneSure")) {
//            intent = new Intent(this, MiBaoActivity.class);
//            finish();
//        } else {
            intent = new Intent(this, BackPwdTelActivity.class);
//        }

        //拿到节点 传递出去
        intent.putExtra("xtdz", Arad.preferences.getString(LocalMark.SERVER_URL.getName()));
        //拿到uid  传递出去
        intent.putExtra("uid", Arad.preferences.getString(LocalMark.USER_UID.getName()));
        intent.putExtra("phoneNumber", ycPhoneNumber);//隐藏后的
        intent.putExtra("pNumber",getIntent().getStringExtra("BoundedPhoneNumber"));//没隐藏的
        intent.putExtra("way", "000");//用来标志我是从设置点击进来的
        startActivityForResult(intent, 1234);
        AnimUtil.intentSlidIn(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        switch (requestCode) {
//            case 123://点击密保跳转后验证密保问题成功
//                if (resultCode == RESULT_OK) {
//                    Log.d("我这儿呢");
//                    goToMiBao.setVisibility(View.GONE);
//                    Arad.preferences.putString("miBaoOnBound", "miBaoSure");
//                    Arad.preferences.flush();
//                }
//                break;
//            case 1234://点击手机跳转后验证手机哈成功后
//                if (resultCode==RESULT_OK) {
//                    goToCellphone.setVisibility(View.GONE);
//                    Arad.preferences.putString("phoneOnBound", "phoneSure");
//                    Arad.preferences.flush();
//                }
//                break;
//        }
    }
}
